﻿using HslCommunication;
using HslCommunication.Profinet.Omron;
using System;

namespace NDK.AcqDevice
{
    public class OmronPLCFins
    {
        private OmronFinsNet omronFinsNet;
        public bool ConnectStatus = false;
        public OmronPLCFins()
        {

        }

        public bool Connect(string IP, int prot)
        {
            omronFinsNet = new OmronFinsNet();
            omronFinsNet.IpAddress = IP;
            omronFinsNet.Port = prot;
            OperateResult operateResult = omronFinsNet.ConnectServer();
            ConnectStatus = operateResult.IsSuccess;
            return operateResult.IsSuccess;
        }

        public bool DisConnect(string IP, int prot)
        {
            OperateResult operateResult = omronFinsNet.ConnectClose();
            ConnectStatus = false;
            return operateResult.IsSuccess;
        }

        public int ReadInt32(string address)
        {
            if (ConnectStatus)
            {
                OperateResult<int> operateResult = omronFinsNet.ReadInt32(address);
                return operateResult.Content;
            }
            return 0;
        }
        public int ReadInt16(string address)
        {
            if (ConnectStatus)
            {
                OperateResult<ushort> operateResult = omronFinsNet.ReadUInt16(address);
                return operateResult.Content;
            }
            return 0;
        }

        public bool WriteInt16(string address, Int16 value)
        {
            if (address == null || address == "XXX")
            {
                return false;
            }

            if (ConnectStatus)
            {
                OperateResult operateResult = omronFinsNet.Write(address, value);
                return operateResult.IsSuccess;
            }
            return false;
        }

    }
}
